package com.shiyi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 这4种方式都可以进行数据的设置，并能够进行回显，并且数据都是存入request中的
 * 上面三种方式虽然使用的使不同的参数设置的数据，但是实际上都会转为同一个对象BindingAwareModelMap
 * @author wangzhanshuai
 * @date 2021/1/21 9:28 下午
 * @since 1.0
 */
@Controller
@RequestMapping("/response")
public class ResponseController {

    @RequestMapping("testResponse")
    public String testResponse(Map<String, String> map) {
        System.out.println(map.getClass());
        map.put("name", "li si");
        return "response";
    }

    @RequestMapping("testResponse2")
    public String testResponse2(ModelMap modelMap) {
        System.out.println(modelMap.getClass());
        modelMap.addAttribute("name", "li si");
        return "response";
    }

    @RequestMapping("testResponse3")
    public String testResponse3(Model model) {
        System.out.println(model.getClass());
        model.addAttribute("name", "li si");
        return "response";
    }

    @RequestMapping("testResponse4")
    public ModelAndView testResponse4() {
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(modelAndView.getClass());
        modelAndView.addObject("name", "li si");
        modelAndView.setViewName("response");
        return modelAndView;
    }
}
